<?php


namespace app\admin\controller;


use app\admin\model\ArchivesDeceasedModel;
use app\admin\model\MaintenanceProjectModel;
use app\admin\model\OrderDepositModel;
use app\admin\model\OrderGoodsModel;
use app\admin\model\OrderModel;
use app\admin\model\OrderPayLogModel;
use app\admin\model\PayRefundModel;
use app\admin\model\ProtectPaymentModel;
use app\admin\model\RetreatTombModel;
use app\admin\model\ServiceBuriedModel;
use app\admin\model\ServiceDepositModel;
use app\admin\model\ServiceMoveModel;
use app\admin\model\ServiceNoModel;
use app\admin\model\ServiceWeibaoModel;
use app\admin\model\TombareaInformationProductModel;
use cmf\controller\AdminBaseController;
use phpDocumentor\Reflection\Type;
use think\Db;

class PrintController extends AdminBaseController
{

    protected $payId =0;//支付id
    protected $type =0;//1定金2结算3售后4商品
    protected $itype =0;//1寄存(定金退款)，售后:2维保3落葬业务4维护费5寄存续费6墓碑刻制7挂失证件工本费8迁墓9单独买商品10退墓11换碑收入12石材维保收入
    protected $orderId =0; // 订单 id
    protected $otherId =0;//对应的表id
    protected $customerId =0;//购墓人id
    protected $pay_info =[];//购墓人id
    protected $refundId =0;//购墓人id
    protected $temp_tx ='';//购墓人id

    protected $cate_arr=[
        1=>'商品墓',
        2=>'艺术墓',
        3=>'生态墓',
        4=>'节地墓'
    ];
    protected $regulation_arr=[
        0=>'',
        1=>'单穴',
        2=>'双穴',
        3=>'三穴',
    ];
    protected $col_status=[
        0=>'无产品',
        1=>'空闲',
        2=>'已认购',
        3=>'定金',
        4=>'无规划',
        5=>'购买未使用',
        6=>'部分安葬',
        7=>'全部安葬',
        8=>'迁出',
    ];
    protected $logo_img = " ";

    public function  initialize()
    {
        //财务打印
        $this->refundId = $this->request->param('refund_id','0');
        $this->payId = $this->request->param('pay_id','0');
        if ( $this->payId ==0){

        }else{
            $this->get_pay_info($this->payId);
            $this->assign('logo_img',$this->logo_img);
        }

        $this->temp_tx = config('temp_tx.text');

        parent::initialize();
    }

    public function get_pay_info($payId){
        $this->pay_info = OrderPayLogModel::where(['id'=>$payId])->find();
        $this->type = $this->pay_info['type'];
        $this->itype = $this->pay_info['itype'];
        $this->orderId = $this->pay_info['order_id'];
        $this->otherId = $this->pay_info['other_id'];
        $this->customerId = $this->pay_info['customer_id'];
        $this->assign('handle_name', cmf_get_admin_name($this->pay_info['handle'])); // 经办人
        $this->assign('pay_user', cmf_get_admin_name($this->pay_info['pay_real_name'])); // 收款人
        $this->assign('pay_time', date('Y.d.m',empty($this->pay_info['collect_time'])?$this->pay_info['collect_time']:time())); // 收款时间

    }

    public function  print_cert(){

        $type=$this->request->param('type');
        $mark=$this->request->param('mark',1);
        $this->otherId = $this->request->param('id',0);
        $itype = $this->request->param('itype',0);
        if($mark==1) {
            $ret = $this->finan_print($type);
//            return $this->fetch($ret['html']);
        }else if($mark==2) {
            $this->service_print($type);
        }else if($mark==3) {
            $ret = $this->refund_print($type);
//            return $this->fetch($ret['html']);
        }else{
            $this->preview($type,$itype);
        }
    }
    //财务打印
    public function finan_print($type){
        $result_array['type'] = $type;
        $result_array['itype'] = $this->itype;
        $this->pay_info = OrderPayLogModel::where(['id'=>$this->payId])->find();
        $this->type = $this->pay_info['type'];
        $this->itype = $this->pay_info['itype'];
        $this->orderId = $this->pay_info['order_id'];
        $this->otherId = $this->pay_info['other_id'];
        $this->customerId = $this->pay_info['customer_id'];
        $this->assign('handle_name', cmf_get_admin_name($this->pay_info['handle'])); // 经办人
        $this->assign('pay_user', cmf_get_admin_name($this->pay_info['pay_real_name'])); // 收款人
        $this->assign('pay_time', date('Y.d.m',empty($this->pay_info['collect_time'])?$this->pay_info['collect_time']:time())); // 收款时间
        $result=[];
        if ($type!=4){
            $result = $this->getorderinfo($this->customerId,0);
        }
        $this->assign('result',$result);

        $tr_html='';
        $result_list =[];
        $total_price=0;
        $table_temp_tx='';
        $table_html = 'table4';
        switch ($type){
            case 1:
                $order_info =  OrderModel::where(['order_id'=>$this->orderId])->find();
                $tomb_type = '定金';
                if ($order_info['tomb_type']== 2 ){
                    $tomb_type = '预付款';
                }
                $table_temp_tx = $this->temp_tx[10];
               //定金收据
                $info['name']= $tomb_type;
                $info['num']='1';
                $info['unit']='';
                $info['unitprice']=$this->pay_info['total_price'];
                $info['free_price']='0.00';
                $info['price']=$this->pay_info['total_price'];
                $info['remark'] = $this->pay_info['remark'];
                $total_price +=$this->pay_info['total_price'];
                array_push($result_list, $info);
                $result_array['head_title'] = '定金收据';
                $this->assign('head_title', '定金收据');
                $this->assign('body_top_name', '收款明细');
                $this->assign('operate_name', $tomb_type);
//                $table_html = 'table1';
                break;
            case 2:
                switch ($this->itype){
                    case 0:
                        $table_temp_tx = $this->temp_tx[20];
                        //销售结算单
                        $result_goods = Db::name('order_goods')->where('order_id', $this->orderId)->select()->toArray();
                        $this->assign('goods', $result_goods);
                        $goods_pricearr = array_column($result_goods, 'total_price');

                        $info['name'] ='购墓';
                        $info['unitprice'] =$this->pay_info['tom_price'];
                        $info['num'] = 1;
                        $info['unit'] = '套';
                        $info['free_price'] = $result['selling_price']-$result['closing_price'];
                        $info['price'] =$result['closing_price'] ;
                        $info['remark'] = '';
                        $total_price +=$result['closing_price'];
                        array_push($result_list, $info);

                        foreach ($result_goods as $key=>&$val){
                            $info['name'] =$val['goods_name'];
                            $info['num'] = $val['num'];
                            $info['unit'] = '';
                            $info['unitprice'] =$val['goods_price'];
                            $info['free_price'] = $val['discount_price'];
                            $info['price'] =$val['total_price'] ;
                            $info['remark'] = '';
                            $total_price +=$val['total_price'];
                            array_push($result_list, $info);
                        }


                        $result_array['head_title'] = '销售结算单';
                        $this->assign('head_title', '销售结算单');
                        $this->assign('body_top_name', '购买结算');
                        $this->assign('operate_name', '购墓');
                        for ($i=1;$i<16-count($result_list);$i++){
                            $tr_html.=' <tr><td colspan="8"></td></tr>';
                        };
                        break;
                    case 1:
                        $table_temp_tx = $this->temp_tx[35];
                        //寄存
                        $jc_ifo =OrderDepositModel::where(['id'=>$this->otherId])->find();
                        $info['name']='寄存';
                        $info['num']=$jc_ifo['storage_time'];
                        $info['unit']='年';
                        if (!empty($jc_ifo['discount_duration'])){
                            $info['unitprice']='1500.00';
                            $info['free_price']=0;
                            $info['remark'] = '';
                        }else{
                            $info['unitprice']=$this->pay_info['tom_price'];
                            $info['free_price'] = ($jc_ifo['storage_time'] * $jc_ifo['original_price'])- $jc_ifo['total_price'] ;
                            $nian =  $info['free_price']/$jc_ifo['original_price'];
                            $info['remark'] =  $nian!=0?"免费寄存{$nian}年":'无' ;
                        }
                        $info['price']=$this->pay_info['total_price'];
                        $total_price +=$this->pay_info['total_price'];
                        array_push($result_list, $info);
                        $result_array['head_title'] = '寄存收据';
                        $this->assign('body_top_name', '寄存结算');
                        $this->assign('head_title', '寄存收据');
                        $this->assign('operate_name', '寄存');
//                        $table_html = 'table3';
                        break;
                }
                break;
            case 3:
                switch ($this->itype) {
                    case 2:
                        $table_temp_tx = $this->temp_tx[32];
                        $wb_list =  MaintenanceProjectModel::where(['wb_id'=>$this->otherId])->select()->toArray();
                        foreach ($wb_list as $key=>&$value){
                            $info['name'] = $value['part_name'];
                            $info['unitprice'] = $value['part_price'];
                            $info['num'] =  $value['duration'];
                            $info['unit'] = $value['unit'];
                            $info['free_price'] =  $value['fee_reduction'];
                            $info['price'] = $value['total_price'] ;
                            if ($value['warranty_type']==1){
                                $info['remark'] = '维修';
                            }elseif ($value['warranty_type']==3){
                                $info['remark'] = '更换';
                            }
                            $total_price +=$value['total_price'];
                            array_push($result_list, $info);
                        }

                        for ($i=1;$i<20-count($result_list);$i++){
                            $tr_html.=' <tr><td colspan="8"></td></tr>';
                        };
                        $result_array['head_title'] = '维保结算单';
                        $this->assign('head_title', '维保结算单');
                        $this->assign('operate_name', '售后-常规维保');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 2);
                        break;
                    case 3:
                        $table_temp_tx = $this->temp_tx[33];
                        //落葬
                        $buried_list = Db::name('pay_project')->where(['buried_id'=>$this->otherId])->select();
                        foreach ($buried_list as $key=>&$value) {
                            $info['name'] = $value['project_name'];
                            $info['unitprice'] = $value['unit_price'];
                            $info['num'] =  $value['num'];
                            $info['unit'] = $value['project_unit'];
                            $info['free_price'] =  $value['free_price'];
                            $info['price'] = $value['total_price'] ;
                            $info['remark'] = '';
                            if ($value['discount_rate']){
                                $info['free_price'] = $value['discount_rate']."%";
                            }
                            $total_price +=$value['total_price'];
                            array_push($result_list, $info);
                        }

                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-落葬');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 3);
                        break;
                    case 4:
                        $table_temp_tx = $this->temp_tx[34];
                        //维护
                        $ProtectPayModel = new ProtectPaymentModel();
                        $list = $ProtectPayModel ->where(['customer_id'=>$this->customerId,'pay_id'=>$this->payId])->order('id desc')->select();
                        foreach($list as $key=>$vo){
                            $info['name']='维护费用';
                            $info['unitprice'] = $vo['manager_price'];
                            $info['num'] =  $vo['duration'];
                            $info['unit'] = '年';
                            $info['free_price'] =  $vo['free_reduce'];
                            $info['price'] = $vo['total_price'] ;
                            $info['remark'] = '';
                            $total_price +=$vo['total_price'];
                            array_push($result_list, $info);
                        }
                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-维护费用');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 4);
                        break;
                    case 5:
                        $table_temp_tx = $this->temp_tx[35];
                        //寄存续费
                        $data_send = Db::name('order_deposit_send')->where('id', $this->otherId)->find();
                        $info['name'] = '寄存续费';
                        $info['unitprice'] = $this->pay_info['tom_price'];
                        $info['unit'] = '年';
                        $info['num'] = $data_send['storage_time'] ;
                        $info['free_price'] = $this->pay_info['free_price'];
                        $info['price'] = $this->pay_info['actual_amount'];
                        $nian = intval(($data_send['total_price'] - $data_send['original_price']) / $data_send['original_price']);
                        $info['remark'] =  '';

                        $info['price']=$this->pay_info['total_price'];
                        $total_price +=$this->pay_info['total_price'];
                        array_push($result_list, $info);
                        $result_array['head_title'] = '寄存续费收据';
                        $this->assign('head_title', '寄存续费收据');
                        $this->assign('operate_name', '收款明细');
                        $table_html = 'table3';
                        $result_array['type'] = 2;
                        $result_array['itype'] =1;
                        break;
                    case 6:
                        $table_temp_tx = $this->temp_tx[36];
                        //墓碑刻制
                        $data = Db::name('carve')->where('id', $this->otherId)->find();
                        $carve_arr = explode(',', $data['type']);
                        //碑文制作
                        if (in_array('insc', $carve_arr)) {
                            $insc_info = Db::name('carve_insc')->where('carve_id', $data['id'])->find();
                            $remark_position = '';
                            foreach (explode(',',$insc_info['position']) as $key){
                                if ($key==1)$remark_position.= '正面,';
                                elseif ($key==2)$remark_position.= '背面,';
                                elseif ($key==3)$remark_position.= '盖板,';
                                elseif ($key==4)$remark_position.= '局部';
                            }

                            $type = $insc_info['type']==1?'首刻':($insc_info['type']==2?'补刻':($insc_info['type']==3?'重刻':''));
                            $insc['name'] = '碑文刻制-'.$type;
                            $insc['num'] = 1;
                            $insc['unitprice'] = $insc_info['price'];
                            $insc['unit'] = '';
                            $insc['price'] = $insc_info['price'];
                            $insc['remark'] = $remark_position;
                            $total_price+=$insc_info['price'];
                            array_push($result_list, $insc);
                        }
                        //字体贴金
                        if (in_array('gold', $carve_arr)) {
                            $gold_info = Db::name('carve_gold')->where('carve_id', $data['id'])->find();
                            $gold['name'] = '字体贴金';
                            $gold['num'] = 1;
                            $gold['unitprice'] = $gold_info['unitprice'];
                            $gold['unit'] = $gold_info['unit']==1?'平方米':($gold_info['unit']==2?'套':'');
                            $gold['price'] = $gold_info['price'];
                            $gold['remark'] = $gold_info['type']==1?'字体贴金':($gold_info['type']==2?'描金漆':'');
                            $total_price+=$gold_info['price'];
                            array_push($result_list, $gold);
                        }
                        //磁像制作image
                        if (in_array('image', $carve_arr)) {
                            $image_info = Db::name('carve_image')->where('carve_id', $data['id'])->find();
                            $image['name'] = '瓷像制作';
                            $image['num'] = 1;
                            $image['unitprice'] = $image_info['price'];
                            $image['unit'] = $image_info['shape']==1?'矩形':($image_info['shape']==2?'椭圆形':'');
                            $image['price'] = $image_info['price'];
                            $image['remark'] = $image_info['specificat'].'吋';
                            $total_price+=$image_info['price'];
                            array_push($result_list, $image);
                        }

                        for ($i=1;$i<21-count($result_list);$i++){
                            $tr_html.=' <tr><td colspan="8"></td></tr>';
                        };
                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-墓碑刻制');
                        $this->assign('body_top_name', ' 结算明细');
                        $this->assign('is_t', 6);
                        break;
                    case 7:
                        $table_temp_tx = $this->temp_tx[37];
                        //挂失证件工本费
                        $log_list = Db::name('change')->alias('ad')
                            ->field('ad.*,u.user_nickname,c.cart_upload as old_cart_upload')
                            ->leftJoin('dj_user u','u.id = ad.add_user')
                            ->leftJoin('dj_customer_info c','c.id = ad.customer_id')
                            ->where(['ad.id'=>$this->otherId])
                            ->select()->toArray();
                        foreach ($log_list as $key => $val){
                            $log_list[$key]['type_name'] = $val['type'] == 1?"变更":"挂失";
                            $log_list[$key]['change_reason'] = $val['change_reason'] == 0?"原因1":"-";
                            $info['name']=$log_list[$key]['type_name'].'证件工本费';
                            $info['unitprice']=$val['cost'];
                            $info['unit']='本';
                            $info['num']=1;
                            $info['price']=$val['cost'];
                            $info['remark'] = '';
                            $total_price+=$val['cost'];
                            array_push($result_list, $info);
                        }
                        for ($i=1;$i<20-count($result_list);$i++){
                            $tr_html.=' <tr><td colspan="8"></td></tr>';
                        };
                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-挂失证件');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 7);
                        break;
                    case 8:
                        $table_temp_tx = $this->temp_tx[38];
                        //迁墓
                        $move_list = ServiceMoveModel::where(['id'=>$this->otherId])->select();
                        foreach ($move_list as $key => $val) {
                            $info['name'] = '迁墓';
                            $info['unitprice'] = $val['move_money'];
                            $info['unit'] = '';
                            $info['num'] = 1;
                            $info['price'] = $val['move_money'];
                            $info['remark'] = $val['move_reason'];
                            $total_price += $val['move_money'];
                            array_push($result_list, $info);
                        }
                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-迁墓');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 0);

                        break;
                    case 11:
                        $table_temp_tx = $this->temp_tx[32];
                        $wb_list =  MaintenanceProjectModel::where(['wb_id'=>$this->otherId])->select()->toArray();
                        foreach ($wb_list as $key=>&$value){
                            $info['name'] = $value['part_name'];
                            $info['unitprice'] = $value['part_price'];
                            $info['num'] =  $value['duration'];
                            $info['unit'] = $value['unit'];
                            $info['free_price'] =  $value['fee_reduction'];
                            $info['price'] = $value['total_price'] ;
                            if ($value['warranty_type']==1){
                                $info['remark'] = '维修';
                            }elseif ($value['warranty_type']==3){
                                $info['remark'] = '更换';
                            }
                            $total_price +=$value['total_price'];
                            array_push($result_list, $info);
                        }
                        $result_array['head_title'] = '服务结算单';
                        $this->assign('head_title', '服务结算单');
                        $this->assign('operate_name', '售后-以旧换新');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 2);
                        break;
                    case 12:
                        $table_temp_tx = $this->temp_tx[32];
                       $wb_list =  MaintenanceProjectModel::where(['wb_id'=>$this->otherId])->select()->toArray();
                       foreach ($wb_list as $key=>&$value){
                           $info['name'] = $value['part_name'];
                           $info['unitprice'] = $value['part_price'];
                           $info['num'] =  $value['duration'];
                           $info['unit'] = $value['unit'];
                           $info['free_price'] =  $value['fee_reduction'];
                           $info['price'] = $value['total_price'] ;
                           if ($value['warranty_type']==1){
                               $info['remark'] = '维修';
                           }elseif ($value['warranty_type']==3){
                               $info['remark'] = '更换';
                           }
                           $total_price +=$value['total_price'];
                           array_push($result_list, $info);
                       }
                        $result_array['head_title'] = '维保结算单';
                        $this->assign('head_title', '维保结算单');
                        $this->assign('operate_name', '售后-石材维保');
                        $this->assign('body_top_name', '结算明细');
                        $this->assign('is_t', 2);
                        break;
                }
                break;
            case 4:
                $table_temp_tx = $this->temp_tx[40];
                //商品 单据
                $goods_list = OrderGoodsModel::where(['pay_id'=>$this->payId])->select();
                foreach ($goods_list as $key=>&$value){
                    $info['name'] =$value['goods_name'];
                    $info['unitprice'] =$value['goods_price'];
                    $info['num'] = $value['num'];
                    $info['unit'] = '';
                    $info['free_price'] = $value['discount_price'];
                    $info['price'] = $value['actual_amount'];
                    $info['remark'] = '';
                    $total_price +=$value['actual_amount'];
                    array_push($result_list, $info);
                }

                for ($i=1;$i<19-count($result_list);$i++){
                    $tr_html.=' <tr><td colspan="8"></td></tr>';
                };
                $result_array['head_title'] = '销售商品结算单';
                $this->assign('head_title', '销售商品结算单');
                $this->assign('operate_name', '销售-商品');
                $this->assign('body_top_name', '结算明细');

                $this->assign('result','');
//                $table_html = 'table4';
                break;
            case 6:
                $table_html='table6';
                $rt_info = RetreatTombModel::where(['id'=>$this->otherId])->find();
                $mw_id = $rt_info['mw_id'];
                $tomb_regulation = TombareaInformationProductModel::where(['mw_id'=>$mw_id])->find();
                $this->regulation_arr[$tomb_regulation['tomb_regulation']];
                $info['name'] = '墓穴';
                $info['unitprice'] = $rt_info['closing_price'];
                $info['unit'] = '套';
                $info['num'] = 1;
                $info['free_price'] =   $rt_info['loss_ratio'];
                $info['price'] =  $rt_info['refund_amount'];
                $info['remark'] = $rt_info['move_reason'];
                $total_price +=  $rt_info['refund_amount'];
                array_push($result_list, $info);
                $result_array['head_title'] = '退墓通知单';
                $this->assign('tomb_regulation', $this->regulation_arr[$tomb_regulation['tomb_regulation']]);
                $this->assign('head_title', '退墓通知单');
                $this->assign('operate_name', '售后-退墓');
                $this->assign('body_top_name', ' 退款明细');
                $this->assign('is_t', 0);
                break;
        }
//        $this->assign('num_count', $tr_html);
        $this->assign('table_temp_tx', $table_temp_tx);
        $this->assign('num_count', '');
        $this->assign('result_list', $result_list);

        $this->assign('total_price', number_format($total_price,2));
        $html = $this->fetch($table_html)->getContent();
        $result_array['html'] = $html;
//        return ['html'=>$table_html];
        return $this->result($result_array);
    }
    //
    public function preview($type,$itype=0){
        $result_array['type'] = $type;
        $result_array['itype'] = $this->itype;
        $total_price=0;
        $table_html='';
        $result_list=[];
        switch ($type){
            case 1:
                break;
            case 2:
                break;
            case 3:
                $table_html = 'table4';
                switch ($itype){
                    case 3:
                        $inf =  ServiceBuriedModel::where(['id'=>$this->otherId])->find();
                        $this->payId=$inf['pay_id'];
                        $result = $this->getorderinfo($inf['customer_id'],0);
                        $this->assign('result',$result);
                        $this->get_pay_info($inf['pay_id']);
                        //落葬
                        $buried_list = Db::name('pay_project')->where(['buried_id'=>$this->otherId])->select();
                        foreach ($buried_list as $key=>&$value) {
                            $info['name'] = $value['project_name'];
                            $info['unitprice'] = $value['unit_price'];
                            $info['num'] =  $value['num'];
                            $info['unit'] = $value['project_unit'];
                            $info['free_price'] =  $value['free_price'];
                            $info['price'] = $value['total_price'] ;
                            $info['remark'] = '';
                            if ($value['discount_rate']){
                                $info['free_price'] = $value['discount_rate']."%";
                            }
                            $total_price +=$value['total_price'];
                            array_push($result_list, $info);
                        }
                        $result_array['head_title'] = '维保结算单';
                        $this->assign('head_title', '维保结算单');
                        $this->assign('operate_name', '售后-落葬');
                        $this->assign('body_top_name', ' 服务费用（售后服务的收费项目）');
                        $this->assign('is_t', 0);
                        break;
                    case 4:
                        break;
                }

                break;

        }

        $this->assign('result_list', $result_list);
        $this->assign('logo_img', $this->logo_img);

        $this->assign('total_price', number_format($total_price,2));
        $html = $this->fetch($table_html)->getContent();
        $result_array['html'] = $html;
        return $this->result($result_array);
    }
    //服务
    public function service_print($type){
        $id = $this->request->param('id');
        switch ($type){
            case 'l':
                $model = new ServiceBuriedModel();
                $field = "d.id,d.order_id,d.dead_name,d.dead_id,d.dealer_phone,d.buried_time,d.is_hhd,d.funeral_time,d.add_time,d.customer_id,d.handle_name";
                $list=$model->alias('d')->field($field)->where('d.id',$id)->find();
                $this->get_info($list['order_id']);
                $buried_list = ArchivesDeceasedModel::whereIn('buried_id',$list['id'])->select();
                $jiedanren  = cmf_get_admin_name($buried_list[0]['dep_id']);
                $this->assign([
                    'dead1'=>count($buried_list)>=1?$buried_list[0]['deceased_name']:'',
                    'dead2'=>count($buried_list)>=2?$buried_list[1]['deceased_name']:'',
                    'dead3'=>count($buried_list)>2?$buried_list[2]['deceased_name']:'',
                    'date'=>date('Y-m-d',$list['funeral_time']),
                    'time'=>date('H:i',$list['funeral_time']),
                    'jd_time'=>date('Y-m-d H:i',$list['add_time']),
                    'dealer_phone'=>$list['dealer_phone'],
                    'jdr'=>$jiedanren,
                    'is_hhd'=>"√",
                    'is_swzm'=>"√",
                    'is_bwqk'=>"√",
                    'handle_name'=>$list['handle_name'],
                ]);
                $mb_id = count($buried_list)>=1?$buried_list[0]['mb_id']:'';
                if ($mb_id){
                    $product_mb_info = Db::name('product_mb')->where(['id'=>$mb_id])->find();
                    $this->assign('tomb_regulation_1',$product_mb_info['regulation']==1?'√':''); // 单穴、双穴合墓、多穴合墓
                    $this->assign('tomb_regulation_2',$product_mb_info['regulation']==2?'√':''); // 单穴、双穴合墓、多穴合墓
                    $this->assign('tomb_regulation_3',$product_mb_info['regulation']==3?'√':''); // 单穴、双穴合墓、多穴合墓
                }
                $html = $this->fetch('buried')->getContent();
                $info_no = ServiceNoModel::where(['type'=>'l','other_id'=>$id])->find();
                if ($info_no){
                    $num = $info_no['num'];
                }else{
                    $num = ServiceNoModel::get_sn();
                    ServiceNoModel::insertGetId(['type'=>'l','num'=>$num,'add_time'=>time(),'other_id'=>$id]);
                }
                $result_array['html'] = $html;
                $result_array['year'] = date('Y年m月d日');
                $result_array['no'] = $num;
                return $this->result($result_array);
                break;
            case 'j':
                $jicun = ServiceDepositModel::where(['id'=>$id])->find();

                $deceased_list = Db::name('archives_deceased')->alias('ad')
                    ->field('ad.*,od.deposit_time,od.id deposit_id')
                    ->leftJoin('dj_order_deposit od','od.id = ad.deposit_id')
                    ->where(['ad.tomb_id'=>$jicun['tomb_id'],'od.customer_id'=>$jicun['customer_id']])->find();

                $this->assign([
                    'dead1'=>$jicun['dead_name'],
                    'dead2'=>'',
                    'dead3'=>'',
                    'date'=>date('Y-m-d', $deceased_list['deposit_time']),
                    'time'=>date('H:i',$deceased_list['deposit_time']),
                    'jd_time'=>date('Y-m-d H:i',$jicun['add_time']),
                    'dealer_phone'=>$jicun['dealer_phone'],
                    'jdr'=>$jicun['handle_name'],
                    'is_hhd'=>!empty($deceased_list['cremation_certificate'])?"√":'',
                    'handle_name'=>$jicun['handle_name'],
                ]);
                $this->get_info($jicun['customer_id'],0);
                if ($jicun['type']==1){
                    $html = $this->fetch('deposit_out')->getContent();
                }else{
                    $html = $this->fetch('deposit')->getContent();
                }
                $info_no = ServiceNoModel::where(['type'=>'j','other_id'=>$id])->find();
                if ($info_no){
                    $num = $info_no['num'];
                }else{
                    $num = ServiceNoModel::get_sn('');
                    ServiceNoModel::insertGetId(['type'=>'j','num'=>$num,'add_time'=>time(),'other_id'=>$id]);
                }
                $result_array['html'] = $html;
                $result_array['type'] = $jicun['type'];
                $result_array['year'] = date('Y年m月d日');
                $result_array['no'] = $num;
                return $this->result($result_array);
                break;
            case 'q':
                $model = Db::name("serviceMove");
                $id = $this->request->param('id');
                $list=$model->alias('d')
                    ->field('d.receptionist,d.add_time,d.add_user,d.buyer_phone,d.tombstone_plan,d.handle_name,d.move_time,d.customer_id,u.user_nickname,d.death_name')
                    ->join(config('prefix').'user u','d.receptionist=u.id','left')
                    ->where('d.id',$id)->find();
                $death_name = $list['death_name'];
                if(!empty($death_name)){
                    $buried_list = explode(' ',$death_name);
                    $burid1[0]=count($buried_list)>=1?$buried_list[0]:'';
                    $burid1[1]=count($buried_list)>=2?$buried_list[1]:'';
                    $burid1[2]=count($buried_list)>2?$buried_list[2]:'';
                    $this->assign('burid1',$burid1);
                }

                $this->get_info($list['customer_id'],0);

                $buried_list = explode(' ',$death_name);
                $this->assign([
                    'dead1'=>count($buried_list)>=1?$buried_list[0]:'',
                    'dead2'=>count($buried_list)>=2?$buried_list[1]:'',
                    'dead3'=>count($buried_list)>2?$buried_list[2]:'',
                    'date'=>date('Y-m-d',$list['move_time']),
                    'time'=>date('H:i',$list['move_time']),
                    'jd_time'=>date('Y-m-d',$list['add_time']),
                    'dealer_phone'=>$list['buyer_phone'],
                    'jdr'=>cmf_get_admin_name($list['receptionist']),
                    'is_hhd'=>"√",
                    'is_swzm'=>"√",
                    'is_bwqk'=>$list['tombstone_plan'],
                    'handle_name'=>$list['handle_name'],
                ]);

                $info_no = ServiceNoModel::where(['type'=>'q','other_id'=>$id])->find();
                if ($info_no){
                    $num = $info_no['num'];
                }else{
                    $num = ServiceNoModel::get_sn();
                    ServiceNoModel::insertGetId(['type'=>'q','num'=>$num,'add_time'=>time(),'other_id'=>$id]);
                }
                $html = $this->fetch('move')->getContent();
                $result_array['html'] = $html;
                $result_array['year'] = date('Y年m月d日');
                $result_array['no'] = $num;
                return $this->result($result_array);
                break;
            case 'w':
                $WeibaoModel    = new ServiceWeibaoModel();
                $MaintenanceProjectModel    = new MaintenanceProjectModel();
                $id = $this->request->param('id',99);
                $mb = $this->request->param('mb',0);
                //维护详情数据
                if ($mb==1){
                    $list = $MaintenanceProjectModel->alias('wp')
                        ->field('wp.*,pm.pro_name')
                        ->leftJoin(config('prefix').'product_mb pm','wp.pro_id = pm.id')
                        ->where(['wp.wb_id'=>$id])->select();
                }else{
                    $list = $MaintenanceProjectModel->where(['wb_id'=>$id])->select();
                }
                //维保时间和接单人
                $weibao=$WeibaoModel->alias('d')->field(
                    'd.id,d.deliver_time,d.handle_id,d.handle_name,d.service_status,d.add_user,d.type,d.customer_id,d.receptionist,d.add_time'
                )->where(['d.id'=>$id])->find();
                if(!empty($weibao['deliver_time'])) {
                    $weibao['deliver_time'] = date('Y-m-d', $weibao['deliver_time']);
                }
                $this->assign('weibao',$weibao);
                //获取共同信息
                foreach ($list as $k=>&$value){
                    if($value['expedited'] == 1){
                        $value['if_jiaji']='是';
                    }else{
                        $value['if_jiaji']='否';
                    }
                    if ($value['total_price'] == 0) {
                        $value['warranty_type']= '更换';
                    }else{
                        $value['warranty_type']= '维修';
                    }
                    $value['deliver_time'] = date('Y-m-d',$value['deliver_time']);
                }
                $tr_html='';
                for ($i=1;$i<13-count($list);$i++){
                    $tr_html.=' <tr> <td> </td><td colspan="2"> </td><td> </td><td> </td><td> </td><td> </td><td colspan="2"> </td> <td colspan="3"></td></tr>';
                }
                $this->assign('list',$list);
                $this->assign('tr_html',$tr_html);

                $buried_list = ArchivesDeceasedModel::where(['customer_id'=>$weibao['customer_id']])->select();
                $burid1=count($buried_list)>=1?$buried_list[0]['deceased_name']:'';
                $burid2=count($buried_list)>=2?$buried_list[1]['deceased_name']:'';
                $burid3=count($buried_list)>2?$buried_list[2]['deceased_name']:'';
                $this->assign([
                    'dead1'=>$burid1,
                    'dead2'=>$burid2,
                    'dead3'=>$burid3,
                    'jd_time'=>date('Y-m-d',$weibao['add_time']),
                    'jdr'=>cmf_get_admin_name($weibao['receptionist']),
                    'handle_name'=>$weibao['handle_name'],
                ]);
                $info_no = ServiceNoModel::where(['type'=>'w','other_id'=>$id])->find();
                if ($info_no){
                    $num = $info_no['num'];
                }else{
                    $num = ServiceNoModel::get_sn();
                    ServiceNoModel::insertGetId(['type'=>'w','num'=>$num,'add_time'=>time(),'other_id'=>$id]);
                }
                $this->get_info($weibao['customer_id'],0);
                $html = $this->fetch('weibao')->getContent();
                $result_array['html'] = $html;
                $result_array['year'] = date('Y年m月d日');
                $result_array['no'] = $num;
                return $this->result($result_array);

                break;
        }

    }
    public function refund_print($type){
        $total_price=0;
        if ($type==1){
//            $pay_refund_info = PayRefundModel::where(['id'=>$this->refundId ])->find();
            $rt_infi  =  RetreatTombModel::where(['id'=>$this->refundId])->find();
            $result_list=[];
            $info['name'] ='墓穴';
            $info['num'] = 1;
            $info['unit'] = '套';
            $info['unitprice'] =$rt_infi['closing_price'];
            $info['free_price'] = $rt_infi['loss_ratio']!=0?($rt_infi['loss_ratio']*100).'%':'全款';
            $info['price'] =$rt_infi['refund_amount'] ;
            $info['remark'] = '';
            $total_price +=$rt_infi['refund_amount'];
            array_push($result_list, $info);

            $this->assign('buyer_name',$rt_infi['certificate_name']);
            $this->assign('result_list',$result_list);
            $this->assign('logo_img',$this->logo_img);
            $this->assign('tomb_regulation','');
            $this->assign('head_title','退墓通知单');
            $this->assign('operate_name','售后-退墓');
            $this->assign('body_top_name','退款明细');
            $this->assign('is_t',0);
            $this->assign('handle_name',$rt_infi['handle_user']); // 经办人
            $this->assign('pay_user', $rt_infi['certificate_name']); // 收款人
            $this->assign('pay_time', date('Y.d.m',time())); // 收款时间
            $tr_html='';
            for ($i=1;$i<21-count($result_list);$i++){
                $tr_html.=' <tr><td colspan="8" style=" "> </td></tr>';
            };
            $this->assign('tr_html',$tr_html);
            $this->assign('total_price', number_format($total_price,2));
//            return ['html'=>'table6'];
            $this->assign('total_price', number_format($total_price,2));
            $html = $this->fetch('table6')->getContent();
            $result_array['html'] = $html;
//        return ['html'=>$table_html];
            return $this->result($result_array);
        }
    }
    //共同的头部尾部信息
    public  function  get_info($id,$type=1){
        if ($type!=0){
            $where = "o.order_id = '{$id}'  ";
        }else{
            $where = "o.customer_id = '{$id}' ";
        }
        $field_ = "ci.buyer_name,p.tomb_num,p.tomb_region,p.tomb_address,p.layer_num,p.tomb_bit_number,";
        $field_.= "p.tomb_num,c.col_status,p.tomb_regulation,p.tomb_region,p.tomb_address,p.layer_num,p.tomb_bit_number";
        $result =  Db::name('order')->alias('o')->field($field_)
            ->join("dj_tombarea_row_col c","o.tomb_id = c.colID")
            ->join("dj_customer_info ci","o.customer_id = ci.id")
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id")
            ->where($where)->find();

        $tomb_region_ceng = $result['tomb_region'].$result['tomb_address'].$result['layer_num'].$result['tomb_bit_number'];
        $this->assign('buyer_name',$result['buyer_name']); //
        $this->assign('tomb_num',$result['tomb_num']); //
        $this->assign('tomb_address',$tomb_region_ceng); //
        $this->assign('col_status_name',$this->col_status[$result['col_status']]); //
        $this->assign('tomb_regulation_1',$result['tomb_regulation']==1?'√':''); // 单穴、双穴合墓、多穴合墓
        $this->assign('tomb_regulation_2',$result['tomb_regulation']==2?'√':''); // 单穴、双穴合墓、多穴合墓
        $this->assign('tomb_regulation_3',$result['tomb_regulation']==3?'√':''); // 单穴、双穴合墓、多穴合墓
    }

    //订单详细信息
    public  function  getorderinfo($order_id,$type=0){
        $OrderModel = new OrderModel();
        $result = $OrderModel->getRefundOrderBy($order_id,$type);
//        $result = $OrderModel->getOrderByCustomerId($order_id,$type);
        $result['pro_status'] = $this->col_status[$result['pro_status']];
        $result['cart_type_name'] =cart_type('cart',$result['cart_type']);
        $result['handle_name'] = cmf_get_admin_name($result['handle']);

        return $result;
    }

    public function buried($id){
        $service_info = ServiceBuriedModel::where(['id'=>$id])->find();
        $result_project = Db::name('pay_project')->where(['buried_id'=>$id])->select();
        $result = $this->getTopInfo($service_info['customer_id']);
        $tr_html='';
        for ($i=count($result_project)+2;$i<9;$i++){
            $tr_html.=' <tr><td style="height: 30pt;" colspan="8"></td></tr>';
        }
        $array_top['result_list'] = $result_project;
        $array_top['html_tr'] = $tr_html;
        $result['handle_name'] = $service_info['handle_name'];

        $this->assign('result',$result);
        $this->assign('result_list',$result_project);
        $this->assign('html_tr',$tr_html);
        return $this->fetch();
    }

    public function muzheng(){
        $type = $this->request->param('type');
        if ($type==1){
            return $this->fetch();
        }else{
            return $this->fetch('muzheng_jc');
        }
    }

    public function hetong(){
        $ty = $this->request->param('ty');
        $oid = $this->request->param('oid');
        $oid = $this->request->param('oid');
        if ($ty =='ht'){

        }elseif ($ty=='xy'){

        }
    }

    //共同的头部尾部信息
    public  function  getTopInfo($customer_id)
    {
        $OrderModel = new OrderModel();
        $result_info = $OrderModel->getRefundOrderBy($customer_id,0);
        $array_top['buyer_name'] = $result_info['buyer_name'];
        $array_top['buyer_phone'] = $result_info['buyer_phone'];
        $array_top['cart_type'] = cart_type('cart',$result_info['cart_type']);
        $array_top['customer_account'] = $result_info['customer_account'];
        $array_top['customer_address'] = $result_info['customer_address'];
        $array_top['contract_sn'] = $result_info['contract_sn'];
        $array_top['tomb_num'] = $result_info['tomb_num'];
        $array_top['tomb_region'] = $result_info['tomb_region'].$result_info['tomb_address'].$result_info['tomb_bit_number'];
        $array_top['tomb_regulation_name'] = $result_info['tomb_regulation_name'];
        $array_top['status_name'] = $result_info['col_status_name'];
        return $array_top;
    }

}